﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using LanceZhang.DBUtility;//请先添加引用
namespace RealTimePriceFetchService.DAL
{
    /// <summary>
    /// 数据访问类StockTransferSubQueueDAL。
    /// </summary>
    public class StockTransferSubQueueDAL
    {
        public StockTransferSubQueueDAL()
        { }
        #region  成员方法
        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(RealTimePriceFetchService.Model.StockTransferSubQueueModel model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@RealTimeInfo", SqlDbType.VarChar),
					new SqlParameter("@HasCopied", SqlDbType.Bit,1)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.RealTimeInfo;
            parameters[2].Value = model.HasCopied;

            DbHelperSQL.RunProcedure("UP_StockTransferSubQueue_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(RealTimePriceFetchService.Model.StockTransferSubQueueModel model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@RealTimeInfo", SqlDbType.VarChar),
					new SqlParameter("@HasCopied", SqlDbType.Bit,1)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.RealTimeInfo;
            parameters[2].Value = model.HasCopied;

            DbHelperSQL.RunProcedure("UP_StockTransferSubQueue_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.RunProcedure("UP_StockTransferSubQueue_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除已复制数据
        /// </summary>
        public void Clear()
        {
            int rowsAffected;
            SqlParameter[] parameters = { };

            DbHelperSQL.RunProcedure("UP_StockTransferSubQueue_Clear", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RealTimePriceFetchService.Model.StockTransferSubQueueModel GetModel(int ID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            RealTimePriceFetchService.Model.StockTransferSubQueueModel model = new RealTimePriceFetchService.Model.StockTransferSubQueueModel();
            DataSet ds = DbHelperSQL.RunProcedure("UP_StockTransferSubQueue_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.RealTimeInfo = ds.Tables[0].Rows[0]["RealTimeInfo"].ToString();
                if (ds.Tables[0].Rows[0]["HasCopied"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["HasCopied"].ToString() == "1") || (ds.Tables[0].Rows[0]["HasCopied"].ToString().ToLower() == "true"))
                    {
                        model.HasCopied = true;
                    }
                    else
                    {
                        model.HasCopied = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到第一个对象实体
        /// </summary>
        public RealTimePriceFetchService.Model.StockTransferSubQueueModel GetTopModel()
        {
            SqlParameter[] parameters = { };

            RealTimePriceFetchService.Model.StockTransferSubQueueModel model = new RealTimePriceFetchService.Model.StockTransferSubQueueModel();
            DataSet ds = DbHelperSQL.RunProcedure("UP_StockTransferSubQueue_GetTopModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.RealTimeInfo = ds.Tables[0].Rows[0]["RealTimeInfo"].ToString();
                if (ds.Tables[0].Rows[0]["HasCopied"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["HasCopied"].ToString() == "1") || (ds.Tables[0].Rows[0]["HasCopied"].ToString().ToLower() == "true"))
                    {
                        model.HasCopied = true;
                    }
                    else
                    {
                        model.HasCopied = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,RealTimeInfo,HasCopied ");
            strSql.Append(" FROM StockTransferSubQueue ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetTopList(int count)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top "+count+" ID,RealTimeInfo,HasCopied ");
            strSql.Append(" FROM StockTransferSubQueue ");
            strSql.Append(" WHERE HasCopied=0 ");//3.19新增

            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "StockTransferSubQueue";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

